<template>
  <div>
    <avue-form v-if="!!activitBusFormOption" ref="avueForm" :option="activitBusFormOption"
               v-model="activitiModel"
               @submit="submitForm"></avue-form>
  </div>
</template>

<script>
import {getModelConfig} from "@/api/start/activiti/modelApi";
import {getTableOption} from "@/api/start/formEngine/formApi";
import {systemDictionary} from "@/const/systemConstant";
import activitiFormUtils from "@/option/activitiFormUtils";
import formDataApi from "@/api/start/formEngine/formDataApi";

import {activitiFomrBaseColumn} from "@/const/systemConstant";
import {getUserInfo} from "@/api/sys/userApi";

export default {

  name: "startActivitiComponent",
  props: {
    modelId: {
      type: String,
      default: null
    },
    userId: {
      type: String,
      default: null
    },
    formBtn: {
      type: Boolean,
      default: true,
    },
    submitAct: {
      type: Function,
      required: true,
    }

  },
  data() {
    return {
      activitiModel: {},
      activitBusFormOption: null,
      tableId: null,
      activitiStartForm: {},
    }
  },
  created() {

  },
  methods: {
    init() {
      this.initFormOption();
      if (!this.userId) return
      getUserInfo(this.userId).then(res => {
        this.activitiModel.dept_id=res.data.data.deptId
      })
    },
    initFormOption() {
      if (!this.modelId) {
        return;
      }
      if (!this.activitiStartForm) {
        this.activitiStartForm = {};
      }
      this.activitiStartForm.userId = this.userId;
      getModelConfig(this.modelId).then(this.analysisForm)
    },
    analysisForm(res) {
      let data = res.data.data;
      if (data.formType && data.formType === systemDictionary.activitiFormTypes.systemDynamicForm) {
        this.tableId = data.formValue
        getTableOption(this.tableId).then(res => {
          let data = res.data.data;
          let formOption = JSON.parse(data);
          formOption = activitiFormUtils.activitStartMainFormCreate(formOption)
          if (!this.formBtn) {
            formOption.menuBtn = false;
          }
          this.activitBusFormOption = formOption;
          this.$emit("formLoadSuccess")
        })
      }
    },
    submit() {
      this.$refs.avueForm.submit();
    },
    resetForm() {
      this.$refs.avueForm.resetForm();
    },
    submitForm(data, done) {
      data.businessKey = "";
      if (this.userId) {
        data[activitiFomrBaseColumn.COLUMN_CREATE_BY] = this.userId;
      }
      let formData = {};
      for (const key in data) {
        if (key.startsWith("$")) {
          continue
        }
        formData[key] = data[key];
      }
      formDataApi.add(this.tableId, formData).then(res => {
        let businessKey = res.data.data;
        return this.startActiviti(businessKey, formData)
      }).then(res => {
        this.$emit("startOk")
      }).finally(() => {
        done();
      })
    },
    startActiviti(businessKey, form) {
      this.activitiStartForm.businessKey = businessKey;
      this.activitiStartForm.formKey = this.tableId;
      this.activitiStartForm.businessVariables = form;
      if (this.userId) {
        this.activitiStartForm.userId = this.userId
      }
      return this.submitAct(this.modelId, this.activitiStartForm);
    },

  }
}
</script>

<style scoped>

</style>
